const getStudentInfo = async ()=>{
  const res = await axios.get('/dashboard')
  // console.log(res.data)
  const {overview,year,salaryData,groupData,provinceData} = res.data
  // console.log(overview)
  // document.querySelector('[name=salary]').innerText = overview.salary
  // document.querySelector('[name=student_count]').innerText = overview.student_count
  // document.querySelector('[name=age]').innerText = overview.age
  // document.querySelector('[name=group_count]').innerText = overview.group_count
  for(let key in overview){
    document.querySelector('[name='+key+']').innerText = overview[key]
  }
  //axios微任务,挂起后执行下面任务再调用setSalaryYearLine
  //渲染折线图
  setSalaryYearLine(year)
  //渲染饼状图
  setSalaryPie(salaryData)
  //渲染柱状图
  setSalaryLine(groupData)
  //薪资分布图
  setSalaryShow(salaryData)
  //地图分布
  initMapChart(provinceData)
}
getStudentInfo()

//折线图
const setSalaryYearLine = (data)=>{
  // console.log(data)
  const monthArr = data.map((item)=>item.month)
  const salaryArr = data.map((item)=>item.salary)
  const line = document.querySelector('#line');
  let myChart = echarts.init(line);
  let option;
  
  option = {
    title: {
      text: '杭州黑马43期',
      top: '3%',
      left: '2%'
    },
    xAxis: {
      type: 'category',
      data:monthArr,
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed'
        }
      },
      axisLabel: {
        color: '#999'
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      top: '20%'
    },
    color: {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 1,
      y2: 0,
      colorStops: [
        {
          offset: 0,
          color: '#499FEE' // 0% 处的颜色
        },
        {
          offset: 1,
          color: '#5D75F0' // 100% 处的颜色
        }
      ],
      global: false // 缺省为 false
    },
    series: [
      {
        data:salaryArr,
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 6
        },
        symbolSize: 10,
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#499FEE' // 0% 处的颜色
              },
              {
                offset: 0.8,
                color: 'rgba(255,255,255,0.2)' // 100% 处的颜色
              },
              {
                offset: 1,
                color: 'rgba(255,255,255,0)'
              }
            ],
            global: false // 缺省为 false
          }
        }
      }
    ]
  };
  
  option && myChart.setOption(option);
}
//饼状图
const setSalaryPie = (data)=>{
  const pieArr = data.map((item)=>{
    return {name:item.label,value:item.g_count+item.b_count}
  })
  const pie = document.querySelector('#pie')
  let myChart = echarts.init(pie);
  let option;

  option = {
    title: {
      text: '班级薪资分布',
      top: 15,
      left: 10,
      textStyle: {
        fontSize: 16
      }
    },
    legend: {
      bottom: '6%',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['50%', '64%'],
        center: ['50%', '45%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        data:pieArr,
      }
    ]
  };

option && myChart.setOption(option);
}
//柱状图
const setSalaryLine = (data)=>{
  // console.log(data)
  const stuName = data[1].map((item)=>item.name)
  const hopeSalary = data[1].map((item)=>item.hope_salary)
  const salary = data[1].map((item)=>item.salary)
  const lines = document.querySelector('#lines')
  let myChart = echarts.init(lines);
  let option;

  option = {
    tooltip: {
      trigger: 'item'
    },
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#34D39A' // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
          }
        ]
      },
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE' // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
          }
        ]
      }
    ],
    grid: {
      left: 70,
      top: 30,
      right: 30,
      bottom: 50
    },
    xAxis: {
      type: 'category',
      data: stuName,
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed'
        }
      }
    },
    axisLabel: {
      color: '#999'
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    series: [
      {
        data:hopeSalary,
        type: 'bar'
      },
      {
        data:salary,
        type: 'bar'
      }
    ]
  };
const btns = document.querySelector('#btns')
//事件委托
btns.addEventListener('click',function(e){
  if(e.target.tagName === 'BUTTON'){
    //排他
    document.querySelector('.btn-blue').classList.remove('btn-blue')
    e.target.classList.add('btn-blue')
    //获取数字内容
    let key = e.target.innerText

    const stuName = data[key].map((item)=>item.name)
    const hopeSalary = data[key].map((item)=>item.hope_salary)
    const salary = data[key].map((item)=>item.salary)
    // console.log(key)
    option.xAxis.data = stuName
    option.series[0].data = hopeSalary
    option.series[1].data = salary

    option && myChart.setOption(option);
  }
})
option && myChart.setOption(option);
}
//薪资分布图
const setSalaryShow = (data)=>{
  // console.log(data)
  const man = data.map((item)=>{
    return {name:item.label,value:item.b_count}
  })
  const woman = data.map((item)=>{
    return {name:item.label,value:item.g_count}
  })
  const gender = document.querySelector('#gender')
  let myChart = echarts.init(gender);
  let option;

  option = {
    title: [
      {
        text: '男女薪资分布',
        left: '3',
        top:'2'
      },
      {
        text: '男生',
        left: '42%',
        top: '45%'
      },
      {
        text: '女生',
        left: '42%',
        bottom: '1%'
      }
    ],
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    
    series: [
      {
        name: '男生',
        type: 'pie',
        radius: [40, 60],
        center: ['50%', '25%'],
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],

        data: man
      },

      {
        name: '女生',
        type: 'pie',
        radius: [40, 60],
        center: ['50%', '72%'],

        data: woman
      }
    ]
  };

option && myChart.setOption(option);
}
//地图分布
const initMapChart = (data) => {
  console.log(data)
// const a= data.find((item)=>{
//   return item === 
      
//   })
// console.log(a)
  
  const myEchart = echarts.init(document.querySelector('#map'))
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 100 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}

